package com.example.tellwin.home.adpater;

import android.content.Context;
import android.content.res.Resources;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.tellwin.R;
import com.example.tellwin.home.bean.QuestionBean;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class KnowledgeAdapter extends RecyclerView.Adapter<KnowledgeAdapter.PopularKnowdegeViewHolder> {

    List<QuestionBean> knowdegeBeans = new ArrayList<>();
    Context context;

    KnowledgeListener knowledgeListener;

    public void setKnowdegeBeans(List<QuestionBean> knowdegeBeans) {
        this.knowdegeBeans = knowdegeBeans;
        notifyDataSetChanged();
    }

    public void setKnowledgeListener(KnowledgeListener knowledgeListener){
        this.knowledgeListener = knowledgeListener;
    }

    @NonNull
    @Override
    public PopularKnowdegeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new PopularKnowdegeViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_popular_knowdege, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull PopularKnowdegeViewHolder holder, int position) {
        QuestionBean bean = knowdegeBeans.get(position);
        Resources resources = context.getResources();
        if(position%3==0){
            holder.itemPopluarKnowdegeView.setBackgroundResource(R.drawable.bg_blue);
        }else if (position%3==1){
            holder.itemPopluarKnowdegeView.setBackgroundResource(R.drawable.bg_green);
        }else {
            holder.itemPopluarKnowdegeView.setBackgroundResource(R.drawable.bg_yellow);
        }
        holder.itemPopluarKnowdegeView.setPadding(resources.getDimensionPixelOffset(R.dimen.dp_15),
                resources.getDimensionPixelOffset(R.dimen.dp_0),
                resources.getDimensionPixelOffset(R.dimen.dp_0),
                resources.getDimensionPixelOffset(R.dimen.dp_11));
        holder.titleTv.setText(bean.getQuestionName());
        holder.contentTv.setText(bean.getIndexContent());
        holder.likeIv.setSelected(bean.getIsLike() == 1);
        holder.likeIv.setOnClickListener(v -> {
            if(knowledgeListener!=null){
                knowledgeListener.onItemLikeClick(position ,bean);
            }
        });
        holder.itemPopluarKnowdegeView.setOnClickListener(v -> {
            if(knowledgeListener!=null){
                knowledgeListener.onItemClick(position , bean);
            }
        });
        holder.shareIv.setOnClickListener(v -> {
            if(knowledgeListener!=null){
                knowledgeListener.onItemShareClick(position , bean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return knowdegeBeans.size();
    }

    class PopularKnowdegeViewHolder extends RecyclerView.ViewHolder {

         RelativeLayout itemPopluarKnowdegeView;
         TextView titleTv;
         TextView contentTv;
         ImageView shareIv;
         ImageView likeIv;
        
        public PopularKnowdegeViewHolder(@NonNull View itemView) {
            super(itemView);
            itemPopluarKnowdegeView = (RelativeLayout) itemView.findViewById(R.id.item_popluar_knowdege_view);
            titleTv = (TextView) itemView.findViewById(R.id.title_tv);
            contentTv = (TextView) itemView.findViewById(R.id.content_tv);
            shareIv = (ImageView) itemView.findViewById(R.id.share_iv);
            likeIv = (ImageView) itemView.findViewById(R.id.like_iv);
        }
    }

    public interface KnowledgeListener{
        void onItemLikeClick(int position , QuestionBean questionBean);
        void onItemShareClick(int position , QuestionBean questionBean);
        void onItemClick(int position , QuestionBean questionBean);
    }
}
